package com.allenkerr;

import com.allenkerr.model.TimeBean;
import com.allenkerr.dao.TimeBeanRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.util.Arrays;
import java.util.List;

/**
 * Created by 小柯 on 17/9/14.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoDbTest05 {

    @Autowired
    private TimeBeanRepository timeBeanDao;

    @Test
    public void test1() {
        LocalTime _4_30 = LocalTime.of(4, 30);
        LocalTime _5_30 = LocalTime.of(5, 30);
        LocalTime _6_30 = LocalTime.of(6, 30);
        LocalTime _7_30 = LocalTime.of(7, 30);

        List<String> $4_5 = Arrays.asList(_4_30.toString(), _5_30.toString());
        List<String> $4_6 = Arrays.asList(_4_30.toString(), _6_30.toString());
        List<String> $4_7 = Arrays.asList(_4_30.toString(), _7_30.toString());

        List<String> $6_7 = Arrays.asList(_6_30.toString(), _7_30.toString());
        List<String> $6_8 = Arrays.asList(_6_30.toString(), _7_30.toString());
        List<String> $6_9 = Arrays.asList(_6_30.toString(), _7_30.toString());


        TimeBean timeBean1 = new TimeBean(1, this.localTimeToInteger(_4_30), Arrays.asList($4_5, $6_7));
        TimeBean timeBean2 = new TimeBean(2, this.localTimeToInteger(_5_30), Arrays.asList($4_5, $6_9));
        TimeBean timeBean3 = new TimeBean(3, this.localTimeToInteger(_6_30), Arrays.asList($6_8, $6_9));

        timeBeanDao.save(timeBean1);
        timeBeanDao.save(timeBean2);
        timeBeanDao.save(timeBean3);
    }

    private long localTimeToInteger(LocalTime localTime) {
        return localTime.getLong(ChronoField.MINUTE_OF_DAY);
    }

    @Test
    public void test2() {
        List<TimeBean> timeBeanList = timeBeanDao.findAll(new Sort(Sort.Direction.DESC, "start"));
        timeBeanList.forEach(System.out::println);
    }


}
